<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>帖子</title>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1">
    <!-- <link rel="shortcut icon" href="/favicon.ico"> -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="stylesheet" href="/css/iconfont.css">
    <link rel="stylesheet" href="/css/homepage/addpost.css">
    <link rel="stylesheet" href="/css/homepage/comment.css">
    <link rel="stylesheet" href="http://g.alicdn.com/msui/sm/0.6.2/css/sm.min.css">
    <link rel="stylesheet" href="http://g.alicdn.com/msui/sm/0.6.2/css/sm-extend.min.css">
    <link rel="stylesheet" href="http://g.alicdn.com/msui/sm/0.6.2/css/??sm.min.css,sm-extend.min.css">
    <!-- <script type='text/javascript' src='http://g.alicdn.com/msui/sm/0.6.2/js/sm-extend.min.js' charset='utf-8'></script> -->
    <style type="text/css">
        .infinite-scroll-preloader {
            margin-top: -20px;
        }

        .swiper-slide img {
            width: 100%;
        }
    </style>
</head>

<body>
    <div class="page">
        <header class="bar bar-nav">
            <a class="button button-link button-nav pull-left" href="javascript:history.back(-1)"
                data-transition='slide-out'>
                <span class="icon icon-left"></span>
                返回
            </a>
            <h1 class="title">发布你的帖子</h1>
        </header>
        <nav class="bar bar-tab">
            <!-- <input type="text" class="pinglun" placeholder="评论一下吧！"> -->
            <a onclick="sendPost()" class="goPinglun">发布帖子</a>
        </nav>

        <div class="content">
            <div class="list-block">
                <ul>
                    <!-- Text inputs -->
                    <li>
                        <div class="item-content">
                            <div class="item-media"><i class="icon icon-form-name"></i></div>
                            <div class="item-inner">
                                <div class="item-title label">标题</div>
                                <div class="item-input">
                                    <input id="postTitle" type="text" placeholder="标题">
                                </div>
                            </div>
                        </div>
                    </li>
                    <li>
                        <div class="item-content">
                            <div class="item-media"><i class="icon icon-form-gender"></i></div>
                            <div class="item-inner">
                                <div class="item-title label">帖子类型</div>
                                <div class="item-input">
                                    <select id="kind">
                                        <option value="b">普通帖子</option>
                                        <option value="a">商家活动</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
            <div class="item-inner">
                <div class="item-input">
                    <textarea id="postData" placeholder="在这里编辑你的帖子"></textarea>
                </div>
            </div>
            <div class="form-group addPerson">
                <div class="addPhoto">+</div>
                <form action="/uploadss" class="addinput" method="post" enctype="multipart/form-data"></form>
                <div class="addShow"></div>
            </div>
        </div>
    </div>

    <script src="https://cdn.staticfile.org/jquery/2.0.0/jquery.min.js"></script>
    <script>

        // 多图片上传
        var addBtn = $('.addPhoto');
        var allImg = $('.addShow');
        var allIpt = $('.addinput');
        var NUMBER = 0;
        //追加元素
        addBtn.click(function () {
            var addIpt = "<input type='file' name='fileImg' class='file' accept = 'image/*' />";
            allIpt.append(addIpt);
            $('[type=file]').eq(NUMBER).click();
            document.getElementsByClassName('file')[NUMBER].onchange = function () {
                var FILE = this.files[0];
                var reader = new FileReader();
                reader.onload = function (e) {
                    var IMG = "<div class=\"imgContainer\"><img src='" + e.target.result + "' /><i class=\"del iconfont icon-delete\"></i></div>";
                    allImg.append(IMG);
                    $("img").unbind();
                    $("img").click(function () {
                        var INDEX = $(this).index();
                        $("input").eq(INDEX)[0].remove();
                        $(this).remove();
                        NUMBER--;
                    });
                    $(".del").on("click", function () {
                        delInd = $(".del").index(this);
                        $(".imgContainer").eq(delInd).remove();
                    });
                };
                reader.readAsDataURL(FILE);
            };
            NUMBER++;
        });

        function sendPost() {
            //获取帖子图片信息
            var formData = new FormData(); 

            for (var i = 0; i < document.getElementsByClassName('file').length; i++) {
                if (document.getElementsByClassName('file')[i].files.length != 0) {
                    formData.append('fileImg', document.getElementsByClassName('file')[i].files[0])
                }
            }
            console.log(formData)
            $.ajax({
                type: 'post',
                url: '/file/uploads',
                data: formData,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function (data) {
                    console.log(data)
                    if (data.err == 0) {
                        
                        var files = new Array();
                        for(let j=0;j<data.data.length;j++){
                            
                            files.push(data.data[j].imgUrl)
                        }console.log(files)

                        const postData = document.getElementById("postData").value;
                        const postTitle = document.getElementById("postTitle").value;
                        const kind = document.getElementById("kind").value;
                        const postImg = document.getElementById("postImg")

                        // console.log(postData + postTitle + kind)
                        $.ajax({
                            type: 'post',
                            url: '/add/upPost',
                            data: {
                                postData: postData,
                                postTitle: postTitle,
                                kind: kind,
                                files: JSON.stringify(files)
                            },
                            dataType: 'json',
                            success: function (data) {
                                console.log(data)
                                if (data.err == 0) {
                                    $.alert(data.msg);
                                    window.location.href = '/homepage'
                                } else {
                                    $.alert(data.msg);
                                }
                            }
                        })
                    } else {
                        alert(data.msg);
                    }
                }
            })
        }


    </script>

    <script type='text/javascript' src='http://g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script>
    <script type='text/javascript' src='http://g.alicdn.com/msui/sm/0.6.2/js/sm.min.js' charset='utf-8'></script>


</body>

</html>